package com.tpc.server.modules.core.document;

/**
 * @version 0.0.1
 * @author frederic.fortin
 * @modified 2010-08-17
 */

import java.sql.ResultSet;
import java.util.logging.Logger;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.tpc.client.modules.core.document.service.DocumentService;
import com.tpc.dao.extend.Document;
import com.tpc.server.systeme.utils.SecuriteManager;
import java.text.SimpleDateFormat;
import java.util.Locale;


@SuppressWarnings("serial")
public class DocumentServiceImpl extends RemoteServiceServlet implements DocumentService {

    private static final long serialVersionUID = 3L;
    private final static Logger objLog = Logger.getLogger(DocumentServiceImpl.class.getName());
    
    public DocumentServiceImpl() {

    }
    
    /** LoadListe **/
    public Document[] loadList(String strId) {
        try {
            Document[] documents = null;
            SecuriteManager securiteManager = new SecuriteManager(this.getThreadLocalRequest());
            if (securiteManager.isAccessAllowed(Document.class.getSimpleName(),"READ")) {
                if (strId != null) {
                    if (strId.equalsIgnoreCase("-1")) {
                        strId = new Integer(securiteManager.getUser().getId()).toString();
                    }
                }
                else {
                    strId = new Integer(securiteManager.getUser().getId()).toString();
                }
                String sqlQuery = "SELECT document.id,document.name,document.description,document.title,document.type,document.path,document.size,document.created_date "
                        + "FROM document,user_has_document WHERE document.life = ? AND document.id = user_has_document.documentId AND user_has_document.userId = ?";
                String[] params = {securiteManager.getProperties().getProperty("ENABLE"),strId};
                ResultSet rs = securiteManager.getDataBaseManager().executeQuery(sqlQuery,params);
                if (rs != null) {
                    int i = 0;
                    int intNbrRow = 0;
                    rs.last();
                    intNbrRow = rs.getRow();
                    documents = new Document[intNbrRow];
                    rs.beforeFirst();
                    while (rs.next()) {
                        /** @PROJECTSPECIFIC  Modify if YOUR PROJECT needs it**/
                        documents[i] = new Document();
                        documents[i].setId(rs.getInt("document.id"));
                        documents[i].setFileName(rs.getString("document.name"));
                        documents[i].setSize(rs.getString("document.size"));
                        documents[i].setDescription(rs.getString("document.description"));
                        documents[i].setTitle(rs.getString("document.title"));
                        documents[i].setPath(rs.getString("document.path"));
                        documents[i].setType(rs.getString("document.type"));
                        SimpleDateFormat sdfYYYMMDD = new SimpleDateFormat(securiteManager.getProperties().getProperty("DATE_FORMAT_VIEW"),Locale.US);
                        String uploadDate = "";
                        if (rs.getDate("document.created_date") != null) {
                            uploadDate = sdfYYYMMDD.format(rs.getDate("document.created_date"));
                        }
                        documents[i].setDateUpload(uploadDate);
                        /** Modify if YOUR PROJECT needs it**/
                        i++;
                    }
                }
                securiteManager.getDataBaseManager().closeConnection();
            }
            return documents;
        }
        catch (Exception objx) {
            objLog.info("message 2: "+objx);
            return null;
        }
    }
        
    /** Load One **/
    public Document load(String strId) {
        try {
            Document Document = null;
            SecuriteManager securiteManager = new SecuriteManager(this.getThreadLocalRequest());
            if (securiteManager.isAccessAllowed(Document.class.getSimpleName(),"READ")) {
                
            }
            securiteManager.getDataBaseManager().closeConnection();
            return Document;
        }
        catch (Exception objx) {
            objLog.info("message 2: "+objx.getMessage());
            return null;
        }
    }
    
    /** Add/Create **/
    public Document create(Document objDocument) {
        try {
            SecuriteManager objSecurite = new SecuriteManager(this.getThreadLocalRequest());
            if (new SecuriteManager(this.getThreadLocalRequest()).isAccessAllowed(Document.class.getSimpleName(),"EXEC")) {
                
                objSecurite.getDataBaseManager().closeConnection();
            }
            return objDocument;
        }
        catch (Exception objx) {
            objLog.info("message 2: "+objx);
            return null;
        }
    }

    /** Update/Modify **/
    public Document modify(Document objDocument) {
        try {
            SecuriteManager objSecurite = new SecuriteManager(this.getThreadLocalRequest());
            if (new SecuriteManager(this.getThreadLocalRequest()).isAccessAllowed(Document.class.getSimpleName(),"EXEC")) {
                
                objSecurite.getDataBaseManager().closeConnection();
            }
            return objDocument;
        }
        catch (Exception objx) {
            objLog.info("message 2: "+objx);
            return null;
        }
    }

    /** Remove/Delete **/
    public int delete(String[] arrstrId) {
        try {
            int success = 1;
            SecuriteManager securiteManager = new SecuriteManager(this.getThreadLocalRequest());
            if (new SecuriteManager(this.getThreadLocalRequest()).isAccessAllowed(Document.class.getSimpleName(),"EXEC")) {
                
            }
            securiteManager.getDataBaseManager().closeConnection();
            return success;
        }
        catch (Exception objx) {
            objLog.info("message 2: "+objx);
            return -1;
        }
    }
    
    

}